package control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.UsuarioCT;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dao.UsuarioCTDAO;

public class LoginAction extends Action {
	
	public ActionForward execute(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws SQLException {
		
		LoginForm formulario = (LoginForm)form;
		UsuarioCT usuarioForm = formulario.getUsuario();
		
//		UsuarioCTDAO dao = UsuarioCTDAO.getInstance();
//		UsuarioCT usuarioCT = dao.getUsuario(usuarioForm.getUsuario());
		
		Facade facade = Facade.getInstance();
		//UsuarioForm - Login e Senha
		UsuarioCT usuarioCT = facade.login(usuarioForm);

		
//		response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
//		response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
//		//response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
//		response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
		String action = "";
		if (usuarioCT != null) {
			
			if (usuarioCT.getUsuario().equals("admin")) 
				action = "logado_admin";
			else action = "logado";
			HttpSession session = request.getSession();
			session.setAttribute("usuario", usuarioCT);
		} 
		else action = "nao_logado";
		
		return map.findForward(action);

	}
}